/**
 * 官解-回溯。我理解就是递归
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
    if (!digits) return []
    let digitLetterMap = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r', 's'], ['t', 'u', 'v'], ['w', 'x', 'y', 'z']],
        result = []

    function letterCom(digits, index, arr) {
        if (digits.length === index) {
            // 每一次递归到底存储
            result.push(arr.join(''))
            return
        } else {
            const letters = digitLetterMap[digits[index] - 2]
            for (let i = 0; i < letters.length; i++) {
                const item = letters[i];
                arr.push(item)
                letterCom(digits, index + 1, arr, digitLetterMap)
                arr.pop()
            }
        }
    }
    letterCom(digits, 0, [])
    return result
};

/**
 * 最简单的方法，穷举
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
    if (!digits) return []
    let digitLetterMap = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r', 's'], ['t', 'u', 'v'], ['w', 'x', 'y', 'z']],
        result = digitLetterMap[digits[0] - 2]

    for (let i = 1; i < digits.length; i++) {
        const letterArray = digitLetterMap[digits[i] - 2];
        let temp = []
        for (let j = 0; j < result.length; j++) {
            const item = result[j];
            for (let m = 0; m < letterArray.length; m++) {
                temp.push(item + letterArray[m])
            }
        }
        result = temp
    }
    return result
};